package com.stadyhelper.service.Impl;

import com.stadyhelper.Utils.Md5Util;
import com.stadyhelper.Utils.ThreadLocalUtil;
import com.stadyhelper.mapper.UserMapper;
import com.stadyhelper.pojo.User;
import com.stadyhelper.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User findByUsername(String username) {
        return userMapper.findUsername(username);
    }

    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updatePwd(Md5Util.getMD5String(newPwd),id);
    }

    @Override
    public void register(String username, String password) {
        String md5word = Md5Util.getMD5String(password);
        userMapper.add(username,md5word);
    }


}
